package ph.com.gs3.formalistics.presenter;

import ph.com.gs3.formalistics.R;
import ph.com.gs3.formalistics.model.valueobjects.business.User;
import ph.com.gs3.formalistics.presenter.fragments.views.UserProfileViewFragment;
import ph.com.gs3.formalistics.presenter.fragments.views.UserProfileViewFragment.UserProfileViewActionListener;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class UserProfileActivity extends Activity implements UserProfileViewActionListener {

	public static final String TAG = UserProfileActivity.class.getSimpleName();

	public static final String EXTRA_USER = "user";
	public static final String EXTRA_LOGOUT_FLAG = "logout_flag";

	public static final int FLAG_NO_LOGOUT = 0;
	public static final int FLAG_DID_LOGOUT = 1;

	private UserProfileViewFragment viewFragment;

	private User userOnDisplay;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_user_profile);

		initializeFieldsFromExtras();

		if (savedInstanceState == null) {
			viewFragment = UserProfileViewFragment.createInstance(userOnDisplay);
			getFragmentManager().beginTransaction().add(R.id.container, viewFragment).commit();
		}
	}

	private void initializeFieldsFromExtras() {

		Bundle extras = getIntent().getExtras();
		userOnDisplay = (User) extras.getSerializable(EXTRA_USER);

	}

	@Override
	public void onLogoutCommand() {
		returnToLoginActivity();
	}

	private void returnToLoginActivity() {

		Intent responseData = new Intent();
		responseData.putExtra(EXTRA_LOGOUT_FLAG, FLAG_DID_LOGOUT);
		setResult(RESULT_OK, responseData);
		finish();

	}

}
